package com.example.myapplication2003;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListViewActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view2);

        // 定义模拟数据
        List<Map<String, Object>> data = new ArrayList<>();
        for(int i=0; i<10; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", i);
            map.put("item", "this is item " + i);
            data.add(map);
        }

        String[] from = {"id", "item"};
        int[] to = {R.id.index, R.id.item};
        // 定义Adapter
        SimpleAdapter simpleAdapter =
                new SimpleAdapter(this,data,R.layout.list_view_item2,from,to);

        // 关联listview
        ListView listView = findViewById(R.id.list_view);
        listView.setAdapter(simpleAdapter);   // 关联

    }
}